﻿using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace Tsanie.ShellExtension {

    /// <summary>
    /// InfoTip Handler 的基类
    /// </summary>
    public abstract class InfoTipHandlerBase : IQueryInfo, IPersistFile {

        #region - 私有字段 -

        private string m_FileName;

        #endregion

        #region - 构造 -

        protected InfoTipHandlerBase() : base() { }

        #endregion

        #region - 公共属性 -

        /// <summary>
        /// 获取文件名
        /// </summary>
        public string Filename { get { return m_FileName; } }

        #endregion

        #region - 子类重写 -

        /// <summary>
        /// 返回提示文本
        /// </summary>
        /// <param name="Flags">Flags directing the handling of the item.</param>
        public abstract string OnGetInfoTip(InfoTipOptions options);
        /// <summary>
        /// 返回InfoFlags
        /// </summary>
        public virtual InfoFlags OnGetInfoFlags() { return InfoFlags.Cached; }

        #endregion

        #region - IPersistFile 成员 -

        void IPersistFile.GetClassID(out Guid pClassID) { pClassID = Guid.Empty; }
        void IPersistFile.GetCurFile(out string ppszFileName) { ppszFileName = null; }
        int IPersistFile.IsDirty() { return unchecked((int)0x80004001); }
        void IPersistFile.Load(string pszFileName, int dwMode) { m_FileName = pszFileName; }
        void IPersistFile.Save(string pszFileName, bool fRemember) { }
        void IPersistFile.SaveCompleted(string pszFileName) { }

        #endregion

        #region - IQueryInfo 成员 -

        void IQueryInfo.GetInfoTip(InfoTipOptions options, out string text) {
            text = OnGetInfoTip(options);
        }
        void IQueryInfo.GetInfoFlags(out InfoFlags flag) {
            flag = OnGetInfoFlags();
        }

        #endregion

        #region - COM 组件注册 -

        [System.Runtime.InteropServices.ComRegisterFunction()]
        private static void ComReg(Type t) {
            Common.RegisterExtension(t, "{00021500-0000-0000-C000-000000000046}");
        }
        [System.Runtime.InteropServices.ComUnregisterFunction()]
        private static void ComUnreg(Type t) {
            Common.UnregisterExtension(t, "{00021500-0000-0000-C000-000000000046}");
        }

        #endregion
    }

}
